/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2022-09-15 10:46:25
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-02-27 11:28:50
 * @FilePath: \robot\src\store\patroling.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { ref, reactive, Ref, computed } from "vue";
import { websocketData, PatrolingInfo } from "@/views/patroling/shared";
import parseTime from "@/utils/parseTime";

// 围界车相关
const usePatrolingStore = defineStore("patroling", () => {
  const map: Ref<Microsoft.Maps.Map> = ref(null);
  const patrolingInfo: PatrolingInfo = reactive({});
  const drawingTools: Ref<Microsoft.Maps.DrawingTools> = ref(null);
  const excutingLayer: Ref<Microsoft.Maps.Layer> = ref(null);
  const previewLayer: Ref<Microsoft.Maps.Layer> = ref(null);
  const carMarks = ref([]);
  const drawingStatus = ref(false);
  const createdPath = ref([]);

  /**
   * @description: 更新围界车数据
   * @param {*} data
   * @return {*}
   */
  function updatePatrolingInfo(data) {
    Object.assign(patrolingInfo, data);
  }

  function startCreatePath() {
    drawingStatus.value = true;
    drawingTools.value.create(
      Microsoft.Maps.DrawingTools.ShapeType.polyline,
      (shape: Microsoft.Maps.Polyline) => {
        shape.setOptions({ strokeColor: "#FF9A00" });
      }
    );
  }

  function updatePathPoint() {
    previewLayer.value.clear();
    const pins = createdPath.value.map((item, index) => {
      const location = new Microsoft.Maps.Location(item.x, item.y);
      return new Microsoft.Maps.Pushpin(location, {
        title: (index + 1).toString(),
        color: "#FF9A00",
      });
    });
    previewLayer.value.add(pins);
  }

  function clearPath() {
    previewLayer.value.clear();
    drawingStatus.value = false;
    createdPath.value = [];
    if (!drawingTools.value) return;
    drawingTools.value.finish();
  }

  function selectTemplate(points) {
    drawingStatus.value = true;
    const locations = points.map((item) => {
      return new Microsoft.Maps.Location(item.x, item.y);
    });
    const line = new Microsoft.Maps.Polyline(locations, {
      strokeColor: "#FF9A00",
    });
    createdPath.value = points;
    drawingTools.value.edit(line);
    updatePathPoint();
  }

  const notifications: websocketData[] = reactive([]);
  function addNotification(data: websocketData) {
    data.time = parseTime(new Date());
    notifications.push(data);
  }

  function removeNotification(index: number) {
    notifications.splice(index, 1);
  }

  // 地图中心点
  const center = computed(() => ({
    latitude: patrolingInfo.latitude || 0,
    longitude: patrolingInfo.longitude || 0,
  }));
  function backToCenter() {
    map.value.setView({
      center: new Microsoft.Maps.Location(
        center.value.latitude,
        center.value.longitude
      ),
    });
  }

  return {
    patrolingInfo,
    updatePatrolingInfo,
    clearPath,
    createdPath,
    selectTemplate,
    notifications,
    addNotification,
    removeNotification,
    map,
    backToCenter,
    drawingTools,
    excutingLayer,
    previewLayer,
    carMarks,
    startCreatePath,
    drawingStatus,
    updatePathPoint,
  };
});

export default usePatrolingStore;
